package hust.hiepdv.javaproject.view;

import hust.hiepdv.javaproject.constant.Constant;
import hust.hiepdv.javaproject.model.Ball;
import hust.hiepdv.javaproject.model.Pad;
import hust.hiepdv.javaproject.model.Player;
import hust.hiepdv.javaproject.networking.TCPServer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSplitPane;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class ServerView implements Constant {

	private JFrame frame;
	private JTextField player1_score;
	private JTextField player2_score;
	private JTextField player3_score;
	private JTextField player4_score;
	private JButton btnReady;
	private JButton btnStart;
	
	public static Ball ball = new Ball(BALL_INIT_X, BALL_INIT_Y, Ball.STYLE_1);
	public static Pad pad1 = new Pad(PAD1_INIT_X, PAD1_Y, Pad.STYLE_1, Pad.HORIZONTAL);
	public static Pad pad2 = new Pad(PAD2_X, PAD2_INIT_Y, Pad.STYLE_2, Pad.VERTICAL);
	public static Pad pad3 = new Pad(PAD3_INIT_X, PAD3_Y, Pad.STYLE_3, Pad.HORIZONTAL);
	public static Pad pad4 = new Pad(PAD4_X, PAD4_INIT_Y, Pad.STYLE_4, Pad.VERTICAL);
	
	public static Player player1 = new Player();
	public static Player player2 = new Player();
	public static Player player3 = new Player();
	public static Player player4 = new Player();
	
	TCPServer tcpServer;
	DrawPanel drawPanel;
	private JPanel panel_1;
	private JTextField player1_state;
	private JTextField player2_state;
	private JTextField player3_state;
	private JTextField textField_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerView window = new ServerView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Server");
		frame.setBounds(100, 100, 890, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		btnReady = new JButton("Ready");
		btnReady.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					tcpServer = new TCPServer();
					Thread thread = new Thread(tcpServer);
					thread.start();
					ServerView.this.btnReady.setEnabled(false);
					ServerView.this.btnStart.setEnabled(true);
					/*startFrame = new StartGameFrame("Bouncing Ball");
					Thread thread = new Thread(startFrame);
					thread.start();*/
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnReady.setBounds(635, 31, 119, 34);
		frame.getContentPane().add(btnReady);
		
		btnStart = new JButton("Start Game");
		btnStart.setEnabled(false);
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tcpServer.getClientCount()<2){
					JOptionPane.showMessageDialog(ServerView.this.frame, "Game need more than one player!");
				} else {
					drawPanel = new DrawPanel(ServerView.this);
					drawPanel.setBounds(5, 8, 600, 600);
					ServerView.this.frame.getContentPane().add(drawPanel);
					Thread thread = new Thread(drawPanel);
					thread.start();
					tcpServer.setIsPlaying(true);
					tcpServer.startSend();
					ServerView.this.btnStart.setEnabled(false);
				}
			}
		});
		btnStart.setBounds(635, 76, 119, 34);
		frame.getContentPane().add(btnStart);
		
		JButton btnStopGame = new JButton("Stop Game");
		btnStopGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				
				tcpServer.stop();
				drawPanel.stop();			
				ServerView.this.btnStart.setEnabled(true);
				reset();
			}
		});
		btnStopGame.setBounds(635, 369, 119, 34);
		frame.getContentPane().add(btnStopGame);
		
		JPanel player4_state = new JPanel();
		player4_state.setBounds(608, 149, 257, 174);
		frame.getContentPane().add(player4_state);
		player4_state.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Player1");
		lblNewLabel.setBounds(20, 30, 46, 14);
		player4_state.add(lblNewLabel);
		
		JLabel lblPlayer = new JLabel("Player2");
		lblPlayer.setBounds(20, 60, 46, 14);
		player4_state.add(lblPlayer);
		
		JLabel lblPlayer_1 = new JLabel("Player3");
		lblPlayer_1.setBounds(20, 90, 46, 14);
		player4_state.add(lblPlayer_1);
		
		JLabel lblPlayer_2 = new JLabel("Player4");
		lblPlayer_2.setBounds(20, 120, 46, 14);
		player4_state.add(lblPlayer_2);
		
		player1_score = new JTextField();
		player1_score.setBounds(107, 26, 50, 20);
		player4_state.add(player1_score);
		player1_score.setColumns(10);
		
		player2_score = new JTextField();
		player2_score.setColumns(10);
		player2_score.setBounds(107, 56, 50, 20);
		player4_state.add(player2_score);
		
		player3_score = new JTextField();
		player3_score.setColumns(10);
		player3_score.setBounds(107, 86, 50, 20);
		player4_state.add(player3_score);
		
		player4_score = new JTextField();
		player4_score.setColumns(10);
		player4_score.setBounds(107, 116, 50, 20);
		player4_state.add(player4_score);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.ORANGE);
		panel_2.setBounds(75, 27, 20, 20);
		player4_state.add(panel_2);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.RED);
		panel_3.setBounds(75, 57, 20, 20);
		player4_state.add(panel_3);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.GREEN);
		panel_4.setBounds(75, 86, 20, 20);
		player4_state.add(panel_4);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.MAGENTA);
		panel_5.setBounds(75, 116, 20, 20);
		player4_state.add(panel_5);
		
		player1_state = new JTextField();
		player1_state.setColumns(10);
		player1_state.setBounds(167, 26, 80, 20);
		player4_state.add(player1_state);
		
		player2_state = new JTextField();
		player2_state.setColumns(10);
		player2_state.setBounds(167, 56, 80, 20);
		player4_state.add(player2_state);
		
		player3_state = new JTextField();
		player3_state.setColumns(10);
		player3_state.setBounds(167, 84, 80, 20);
		player4_state.add(player3_state);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(167, 114, 80, 20);
		player4_state.add(textField_3);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(5, 5, 600, 600);
		frame.getContentPane().add(panel_1);
	}
	
	public void updateScore(){
		this.player1_score.setText(player1.getScore()+"");
		this.player2_score.setText(player2.getScore()+"");
		this.player3_score.setText(player3.getScore()+"");
		this.player4_score.setText(player4.getScore()+"");
	}
	
	public void reset(){
		ball.setData(BALL_INIT_X, BALL_INIT_Y, Ball.STYLE_1);
		pad1.setData(PAD1_INIT_X, PAD1_Y, Pad.STYLE_1, Pad.HORIZONTAL);
		pad2.setData(PAD2_X, PAD2_INIT_Y, Pad.STYLE_2, Pad.VERTICAL);
		pad3.setData(PAD3_INIT_X, PAD3_Y, Pad.STYLE_3, Pad.HORIZONTAL);
		pad4.setData(PAD4_X, PAD4_INIT_Y, Pad.STYLE_4, Pad.VERTICAL);
		
		player1.reset();
		player2.reset();
		player3.reset();
		player4.reset();
	}
}
